using System;
using Bugzproxy;

class ListProductClass {
  static public int Main( string[] args ) {
    ////////////////////////////////////////////////////////////
    // Use common class to get all arguments
    SimpleAppArgs argParser 
      = new SimpleAppArgs( "ListProducts.exe", 
                           "Program to list products" );
    // If parsing fails, bail out
    if ( ! argParser.Parse( args ) ) { 
      return 1; 
    }
    Console.WriteLine( "Using server {0}:{1}/{2}", argParser.Host, argParser.Port, argParser.Path );
    // Console.WriteLine( "Using username {0} and password {1}", argParser.User, argParser.Password );
    // Construct server, based on arguments, setup tracer if user set option
    Server server 
      = new Server( argParser.Host, argParser.Port, argParser.Path );
    if ( argParser.Trace ) { 
      server.TraceWriter = System.Console.Out; 
    }
    
    ////////////////////////////////////////////////////////////
    // Real program starts here
    Console.WriteLine( "Id\tName\tDescription" );
    // We may need a login to list our products, but we try without
    try {
      foreach ( Product p
                in server.GetProducts( server.GetAccessibleProductIds() ) ) {
        Console.WriteLine( p.Id + "\t" + p.Name + "\t" + p.Description );
      }
    }
    catch (Exception e) {
      Console.WriteLine( "An exception occured. Try running with --trace? Exception follows");
      Console.WriteLine( e );
      return 1;
    }
    return 0;
  }
}

